﻿namespace MvcXRepairCustomer.CacheData
{
    using System.Collections.Generic;
    using System.Linq;
    using MvcXRepairCustomer.Interfaces;
    using MvcXRepairCustomer.Services;
    using XRepair.Models;
    using System;

    public class Cache
    {
        private static ISymptomService symptomService;
        private static IBrandService brandService;
        private static IModelService modelService;
        private static IStateService stateService;
        private static ICityService cityService;
        private static Dictionary<string, object> pendingOrders = new Dictionary<string, object>();

        public static List<Symptom> GetAllActiveSymptoms()
        {
            symptomService = new SymptomService();
            try
            {
                return symptomService.GetAllActiveSymptoms().ToList();
            }
            catch (Exception e)
            {
                return new List<Symptom>();
            }

            //return symptomService.GetAllActiveSymptoms().ToList();
        }

        public static bool HasPendingOrderWithKey(string key)
        {
            return pendingOrders.ContainsKey(key);
        }

        public static object WithdrawPendingOrder(string key)
        {
            if (pendingOrders.ContainsKey(key))
            {
                object pendingOrder = pendingOrders[key];
                pendingOrders.Remove(key);

                return pendingOrder;
            }

            return null;
        }

        public static bool SavePendingOrder(string key, object order)
        {
            if (!pendingOrders.ContainsKey(key))
            {
                pendingOrders.Add(key, order);

                return true;
            }

            return false;
        }

        public static List<Brand> GetAllActiveBrands()
        {
            brandService = new BrandService();
            try
            {
                return brandService.GetAllActiveBrands().ToList();
            }
            catch (Exception e)
            {
                return new List<Brand>();
            }

            //return brandService.GetAllActiveBrands().ToList();
        }

        public static List<Model> GetAllActiveModels()
        {
            modelService = new ModelService();
            try
            {
                return modelService.GetAllActiveModels().ToList();
            }
            catch (Exception e)
            {
                return new List<Model>();
            }

            //return modelService.GetAllActiveModels().ToList();
        }

        public static List<State> GetAllActiveStates()
        {
            stateService = new StateService();
            try
            {
                return stateService.GetAllActiveStates().ToList();
            }
            catch (Exception e)
            {
                return new List<State>();
            }
        }

        public static List<City> GetAllActiveCities()
        {
            cityService = new CityService();
            try
            {
                return cityService.GetAllActiveCities().ToList();
            }
            catch (Exception e)
            {
                return new List<City>();
            }
        }
    }
}
